﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xajhzwb.Components.Helper;
using System.Data;
using Xajhzwb.Components.Util;
using System.IO;

namespace Xajhzwb.Components.Plan
{
    public class PlanRunLog
    {
        private XmlHelper m_PlanFile;
        private DataView m_dataLog;

        public PlanRunLog()
        {
            m_dataLog = new DataView();
        }

        public void InsertOnceLog(Const.LogType lType, string PlanID, string PlanName, Const.RunTaskType rType, string FileName, string Para)
        {
            if(!IsExist())
                NewLogFile();

            XmlHelper xmlconfig = new XmlHelper(Utils.StartupPath + "tasks\\plan\\RunLog.xml");

            string strXml = "<LogType>" + lType + "</LogType>" +
                "<PlanID>" + PlanID + "</PlanID>" +
                "<PlanName>" + PlanName + "</PlanName>" +
                "<FileName>" + FileName + "</FileName>" +
                "<FilePara>" + Para + "</FilePara>" +
                "<TaskType>" + rType + "</TaskType>" +
                "<RunTime>" + DateTime.Now.ToString() + "</RunTime>";

            xmlconfig.InsertElement("Logs", "Log", strXml);
            xmlconfig.Save();
            xmlconfig = null;

        }

        public void OpenLogFile()
        {
            if(!IsExist())
                NewLogFile();

            m_PlanFile = new XmlHelper(Utils.StartupPath + "tasks\\plan\\RunLog.xml");
        }

        public void CloseLogFile()
        {
            m_PlanFile = null;
        }

        public void InsertLog(Const.LogType lType, string PlanID, string PlanName, Const.RunTaskType rType, string FileName, string Para)
        {
            string strXml = "<LogType>" + ((int)lType).ToString() + "</LogType>" +
                "<PlanID>" + PlanID + "</PlanID>" +
                "<PlanName>" + PlanName + "</PlanName>" +
                "<FileName>" + FileName + "</FileName>" +
                "<FilePara>" + Para + "</FilePara>" +
                "<TaskType>" + ((int)rType).ToString() + "</TaskType>" +
                "<RunTime>" + DateTime.Now.ToString() + "</RunTime>";

            m_PlanFile.InsertElement("Logs", "Log", strXml);
            m_PlanFile.Save();

        }

        public void LoadLog()
        {
            if(!IsExist())
                return;

            XmlHelper xmlConfig = new XmlHelper(Utils.StartupPath + "tasks\\plan\\RunLog.xml");

            m_dataLog = xmlConfig.GetData("descendant::Logs");

            xmlConfig = null;

        }

        public int GetLogCount()
        {
            if(m_dataLog == null)
                return 0;
            else
                return m_dataLog.Count;
        }

        public string GetLogType(int index)
        {
            if(m_dataLog == null || m_dataLog.Count == 0)
                return "";
            else
                //return Const.ConvertName(int.Parse(m_dataLog[index].Row["LogType"].ToString()));
                return "";
        }

        public string GetPlanID(int index)
        {
            if(m_dataLog == null || m_dataLog.Count == 0)
                return "";
            else
                return m_dataLog[index].Row["PlanID"].ToString();
        }

        public string GetPlanName(int index)
        {
            if(m_dataLog == null || m_dataLog.Count == 0)
                return "";
            else
                return m_dataLog[index].Row["PlanName"].ToString();
        }

        public string GetFileName(int index)
        {
            if(m_dataLog == null || m_dataLog.Count == 0)
                return "";
            else
                if(int.Parse(m_dataLog[index].Row["TaskType"].ToString()) == (int)Const.RunTaskType.DataTask)
                    //return Const.ConvertName(int.Parse(m_dataLog[index].Row["FileName"].ToString()));
                    return "";
                else
                    return m_dataLog[index].Row["FileName"].ToString();
        }

        public string GetFilePara(int index)
        {
            if(m_dataLog == null || m_dataLog.Count == 0)
                return "";
            else
                return m_dataLog[index].Row["FilePara"].ToString();
        }

        public string GetTaskType(int index)
        {
            if(m_dataLog == null || m_dataLog.Count == 0)
                return "";
            else
                //return Const.ConvertName(int.Parse(m_dataLog[index].Row["TaskType"].ToString()));
                return "";
        }

        public string GetRunTime(int index)
        {
            if(m_dataLog == null || m_dataLog.Count == 0)
                return "";
            else
                return m_dataLog[index].Row["RunTime"].ToString();
        }


        //清空日志信息
        public void DelLog()
        {
            //删除文件再重新建立一个
            File.Delete(Utils.StartupPath + "tasks\\plan\\RunLog.xml");

            NewLogFile();
        }

        private void NewLogFile()
        {
            XmlHelper xmlConfig = new XmlHelper();

            string strXml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
                       "<Logs>" +
                       "</Logs>";
            xmlConfig.NewXmlFile(Utils.StartupPath + "tasks\\plan\\RunLog.xml", strXml);

            xmlConfig = null;

        }

        private bool IsExist()
        {
            return File.Exists(Utils.StartupPath + "tasks\\plan\\RunLog.xml");
        }


    }
}
